/**
 * @author tibork
 */
var editedFieldID = 0;
Ext.namespace("Softab")
Softab.ClientProperties = new Ext.grid.PropertyGrid({
    title: 'Client Properties',
    name: 'ClientPropertiesGrid',
    stripeRows: true,
    source: {}
});
Softab.ClientProperties.on("afteredit", function(e){
    Ext.Ajax.request({
        url: 'server/PropertyGrid.php',
		method: "GET",
        params: {
			__id: editedFieldID,
            save: 'true',
            target: 'DefaultClientProps',
			name: e.record.id,
			value: e.value
        },
		success: function(response){
			//result = Ext.util.JSON.decode(response.responseText);
		}
    })
});


Softab.ServerProperties = new Ext.grid.PropertyGrid({
    title: 'Server Properties',
    name: 'ServerPropertiesGrid',
    stripeRows: true,
    source: {}
});
Softab.ServerProperties.on("afteredit", function(e){
    alert(editedFieldID + "_ServerProps; \r\nnewVal:" + e.value + "; \r\nrecord: " + e.record.id);
});
/* source sample
 "Name": "Properties Grid11",
 "grouping": false,
 "autoFitColumns": true,
 "productionQuality": false,
 "created": new Date(Date.parse('10/15/2006')),
 "tested": false,
 "version": .01,
 "borderWidth": 1
 */
Softab.ClientProperties.LoadSource = function(node, event){
    var fieldId;
    if (node.isLeaf()) {
        fieldId = node.id;
        editedFieldID = node.id;
        Ext.Ajax.request({
            url: 'server/PropertyGrid.php?definition=true&__id=' + fieldId,
            success: function(response){
                propertyStoreDefs = eval(response.responseText);
                var ClientPropertiesStore = new Ext.data.JsonStore({
                    storeId: fieldId + '_ClientProps',
                    autoLoad: true, //autoload the data
                    url: 'server/PropertyGrid.php?ClientProps=true&__id=' + fieldId,
                    root: 'ClientProps',
                    fields: eval(propertyStoreDefs.ClientPropsStoreDef),
                    listeners: {
                        load: {
                            fn: function(store, records, options){
                                if (Softab.ClientProperties) {
                                    Softab.ClientProperties.setSource(store.getAt(0).data);
                                }
                            }
                        }
                    }
                })
                var ServerPropertiesStore = new Ext.data.JsonStore({
                    storeId: fieldId + '_ClientProps',
                    autoLoad: true, //autoload the data
                    url: 'server/PropertyGrid.php?ServerProps=true&__id=' + fieldId,
                    root: 'ServerProps',
                    fields: eval(propertyStoreDefs.ServerPropsStoreDef),
                    listeners: {
                        load: {
                            fn: function(store, records, options){
                                if (Softab.ClientProperties) {
                                    Softab.ServerProperties.setSource(store.getAt(0).data);
                                }
                            }
                        }
                    }
                })
            },
            failure: function(){
                alert("Failure!");
            }
        });
        /** @todo Load the EntityField properties in Softab.PropertyGrid */
    
    }
}

Softab.PropertiesEditor = new Ext.Panel({
    name: 'FieldFormProperties',
    title: 'Field Form Properties',
    layout: 'accordion',
    bodyBorder: false,
    layoutConfig: {
        animate: true
    },
    items: [Softab.ClientProperties, Softab.ServerProperties]
});
